package top.aigoo.project01.controller.shopadmin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.aigoo.project01.entity.ProductCategory;
import top.aigoo.project01.entity.Shop;
import top.aigoo.project01.entity.dto.ProductCategoryExecution;
import top.aigoo.project01.entity.dto.Result;
import top.aigoo.project01.enums.ProductCategoryEnum;
import top.aigoo.project01.service.ProductCategoryService;
import top.aigoo.project01.utils.HttpServletRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商品分类
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/8/25 22:42
 */
@Controller
@RequestMapping("shopadmin")
public class ProductCategoryManagementController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping(value = "/getproductcategorylist", method = RequestMethod.GET)
    @ResponseBody
    private Result<List<ProductCategory>> getProductCategorylist(HttpServletRequest request) {
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        List<ProductCategory> list = null;
        if (currentShop != null && currentShop.getShopId() > 0) {
            list = productCategoryService.queryByShopId(currentShop.getShopId()).getProductCategoryList();
            return new Result<List<ProductCategory>>(true, list);// WEB-INF/html/"shoplist".html
        } else {
            return new Result<List<ProductCategory>>(false, -1001, "没有获取到店铺信息!");
        }
    }

    @RequestMapping(value = "listproductcategorys", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listProductCategorys(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();

        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        List<ProductCategory> list = null;

        if (currentShop != null && currentShop.getShopId() > 0) {
            ProductCategoryExecution pce = productCategoryService.queryByShopId(currentShop.getShopId());
            if (pce.getState() == ProductCategoryEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
                modelMap.put("msg", pce.getStateInfo());
                modelMap.put("list", pce.getProductCategoryList());
            } else {
                modelMap.put("success", false);
                modelMap.put("msg", pce.getStateInfo());
            }

        }
        return modelMap;
    }


    @RequestMapping(value = "addproductcategorys", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryLists, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();
        // 数据初始化
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");

        for (ProductCategory pc : productCategoryLists) {
            pc.setShopId(currentShop.getShopId());
        }

        if (productCategoryLists != null && productCategoryLists.size() > 0) {
            try {
                ProductCategoryExecution pce = productCategoryService.batchAddProductCategory(productCategoryLists);
                if (pce.getState() == ProductCategoryEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", pce.getStateInfo());
                }
            } catch (RuntimeException e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
                return modelMap;
            }

        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "请至少输入一个商品类别");
        }

        return modelMap;
    }


    @RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> removeProductCategory(Long productCategoryId,
                                                      HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();

        if (productCategoryId != null && productCategoryId > 0) {
            try {
                Shop currentShop = (Shop) request.getSession().getAttribute(
                        "currentShop");
                ProductCategoryExecution pe = productCategoryService
                        .deleteProductCategory(productCategoryId,
                                currentShop.getShopId());
                if (pe.getState() == ProductCategoryEnum.SUCCESS
                        .getState()) {
                    modelMap.put("success", true);
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", pe.getStateInfo());
                }
            } catch (RuntimeException e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
                return modelMap;
            }

        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "请至少选择一个商品类别");
        }
        return modelMap;
    }

}
